# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: fengyang

import os

from absl import app, flags
FLAGS = flags.FLAGS


def remove_tpm_files_in_dir(feed_dir):
  if not os.path.exists(feed_dir):
    print("%s not exists" % feed_dir)
    return
  files = os.listdir(feed_dir)
  for filename in files:
    # temporary file name looks like
    # futures_binance_v1_a2.queue-0030xxib0t.20200312-100000Z.gz.9392A08c
    comp = filename.split('.')
    if len(comp) != 5:
      continue
    if comp[3] != 'gz':
      continue
    if len(comp[4]) != 8:
      continue

    full_path = os.path.join(feed_dir, filename)
    print("remove " + full_path)
    os.remove(full_path)


def remove_tmp_files(flow_root_dir, machine, date):
  feed_dir = os.path.join(flow_root_dir, machine, date)
  sub_dirs = next(os.walk(feed_dir))[1]
  sub_dirs = [os.path.join(feed_dir, sub_dir) for sub_dir in sub_dirs]
  for sub_dir in sub_dirs:
    remove_tpm_files_in_dir(sub_dir)


def main(_):
  remove_tmp_files(FLAGS.flow_root_dir, FLAGS.machine, FLAGS.date)


if __name__ == '__main__':
  flags.DEFINE_string('flow_root_dir', '/export/iosg/coin-mirror-1/buckets/feed.raw.coin', 'flow root dir')
  flags.DEFINE_string('machine', None, 'hostname')
  flags.DEFINE_string('date', None, 'yyyymmdd')

  app.run(main)
